from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _

from timezones.fields import TimeZoneField

class Profile(models.Model):
    
    GENDER_CHOICE = (
        ('M',_('Male')),
        ('F',_('Female')),
    )
    
    BLOOD_CHOICE = (
        ('A', _('A')),
        ('B', _('B')),
        ('AB', _('AB')),
        ('O', _('O')),
        ('u', _('Unknown')),
    )
    
    EDU_LEVEL_CHOICE = (
        ('bjc', _('bellow junior college')),
        ('jc', _('junior college')),
        ('b', _('becholar')),
        ('m', _('master')),
        ('d', _('doctor')),
        ('pd', _('post doctor')),
    )
    
    HOUSE_CHOICE = (
        ('lwp',_('live with parents')),
        ('so', ('self owned')),
        ('rent', ('rent')),
    )
    
    #basic information
    user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
    name = models.CharField(_('name'), max_length=50, blank=True)
    gender = models.CharField(_('gender'), max_length=1, choices=GENDER_CHOICE, blank=True)
    birthday = models.DateField(_('birthday'),null=True, blank=True)
    height = models.PositiveSmallIntegerField(_('height'), null=True, blank=True)
    weight = models.PositiveSmallIntegerField(_('weight'), null=True,blank=True)
    residence = models.CharField(_('residence'), max_length=30, blank=True)
    registered_residence = models.CharField(_('registered residence'), max_length=30, blank=True)
    home_town = models.CharField(_('hometown'), max_length=30, blank=True)
    nationality = models.CharField(_('nationality'), max_length=20, blank=True)
    blood_type = models.CharField(_('blood type'), max_length=2, choices=BLOOD_CHOICE, blank=True)
    
    #education
    edu_level = models.CharField(_('education level'), max_length=3, choices=EDU_LEVEL_CHOICE, blank=True)
    school = models.CharField(_('school'), max_length=50, blank=True)
    major = models.CharField(_('major'), max_length=30, blank=True)
    
    #occupation
    industry = models.CharField(_('industry'), max_length=20, blank=True)
    career = models.CharField(_('career'), max_length=30, blank=True)
    salary = models.CharField(_('salary'), max_length=20, blank=True)
    
    #life
    house = models.CharField(_('house'), max_length=4, choices=HOUSE_CHOICE, blank=True)
    car = models.CharField(_('car'), max_length=30, blank=True)
    somke = models.CharField(_('somke'), max_length=30, blank=True)
    drink = models.CharField(_('drink'), max_length=30, blank=True)
    
    #contact
    website = models.URLField(_('website'), null=True, blank=True, verify_exists=False)
    qq = models.IntegerField(_('qq'), max_length=30, null=True, blank=True)
    msn = models.EmailField(_('msn'), blank=True)
    
    about = models.TextField(_('about'), blank=True)
    #location = models.CharField(_('location'), max_length=40, null=True, blank=True)
    
    def __unicode__(self):
        return self.user.username
    
    def get_absolute_url(self):
        return ('profile_detail', None, {'username': self.user.username})
    get_absolute_url = models.permalink(get_absolute_url)
    
    class Meta:
        verbose_name = _('profile')
        verbose_name_plural = _('profiles')

def create_profile(sender, instance=None, **kwargs):
    if instance is None:
        return
    profile, created = Profile.objects.get_or_create(user=instance)

post_save.connect(create_profile, sender=User)
